<?php
class ClusterParam {
	private $message = "";
	function ClusterParam() {

	}

	public function run() {
		$p = '/@param[\s]+([A-Z_]+)[\s]+(.+)/si'; //解析命令参数
		preg_match($p, trim($_POST[Message]), $n);
		if ($n) {
			if ($this->getParamValue($n[1])) {
				if (trim($n[2]) == "-1") {
					DB :: DelTableRow('td_param', array (
						'key' => $n[1],
						'clusterNum' => $_POST[ClusterNum]
					));
					$this->message = "恢复成功~！";
					return;
				}
				if ($n[1] == "ANTI_BRUSH_NUM" && (int) $n[2] <= 2) {
					$this->message = "参数有误~！至少为3";
					return;
				}
				
				$this->saveParamValue($n[1], $n[2]);
				$this->message = "修改成功~！";
				return;
			} else {
				$this->message = "参数类型出错";
				return;
			}
		} else {
			$this->showParam();
		}
	}

	public function getParamValue($param_name, $flag = false) {
		global $cluster_param;
		$ret = @ DB :: LimitQuery('td_param', array (
			'condition' => array (
				'ClusterNum' => $_POST[ClusterNum],
				'key' => $param_name
			)
		), true);
		if ($ret) {
			return $ret[0][value];
		} else {
			if ($flag)
				return false;
			else
				return $cluster_param[$param_name];
		}
	}
	public function saveParamValue($param_name, $value) {
		if (!$this->getParamValue($param_name, true)) {
			$sql = "insert into td_param values ('','{$param_name}','{$value}','{$_POST[ClusterNum]}')";
			DB :: Query($sql);
		} else {
			$sql = "update td_param set `value` = '{$value}' where clusterNum='{$_POST[ClusterNum]}' and `key`='{$param_name}'";
			DB :: Query($sql);
		}
	}

	private function showParam() {
		$this->message = "欢迎使用高级参数设置\n这里设置的是一些机器人在群内使用的一些基本用语和参数,当前参数如下:\n" .
		"参数名称     参数类型 \t\t\t\t\t当前参数值\n" .
		"进群通知语：RECEIVENEWMEMBERADD_MESS \t" . $this->getParamValue('RECEIVENEWMEMBERADD_MESS') . "\n" .
		"退群通知语：QUITFROMCLUSTER_MESS \t\t" . $this->getParamValue('QUITFROMCLUSTER_MESS') . "\n" .
		"踢人通知语：REMOVEDFROMCLUSTER_MESS \t\t" . $this->getParamValue('REMOVEDFROMCLUSTER_MESS') . "\n" .
		"改名通知语：MEMBERCARDCHANGED_MESS \t\t" . $this->getParamValue('MEMBERCARDCHANGED_MESS') . "\n" .
		"改名防刷数：MEMBERCARDCHANGED_NUM \t\t" . $this->getParamValue('MEMBERCARDCHANGED_NUM') . "\n" .
		"刷改名通知：MEMBERCARDCHANGED_ERROR_MESS \t" . $this->getParamValue('MEMBERCARDCHANGED_ERROR_MESS') . "\n" .
		"刷屏通知语：ANTI_BRUSH_MESS \t\t\t" . $this->getParamValue('ANTI_BRUSH_MESS') . "\n" .
		"刷屏判定数：ANTI_BRUSH_NUM \t\t\t" . $this->getParamValue('ANTI_BRUSH_NUM') . "\n" .
		"学习成功语: STUDY_SUCCESS_MESS\t\t\t" . $this->getParamValue('STUDY_SUCCESS_MESS') . "\n" .
		"删除学习语: FORGET_STUDY_MESS\t\t\t" . $this->getParamValue('FORGET_STUDY_MESS') . "\n" .
		"设置管理员: FORGET_STUDY_MESS\t\t\t" . $this->getParamValue('SET_ADMIN_MESS') . "\n" .
		"取消管理员: FORGET_STUDY_MESS\t\t\t" . $this->getParamValue('REMOVE_ADMIN_MESS') . "\n" .
		"加群验证模式: AUTO_ALLOW_MODE\t\t\t" . $this->getParamValue('AUTO_ALLOW_MODE') ." (第一位取值为0,1,2表示验证方式(0,直接同意,1内部验证码验证,2关键词验证),第二位取值0,1(0:验证错误则忽略,1:验证错误则拒绝),第三位取值0,1(0:允许黑名单用户加群,1拒绝黑名单用户加群))". "\n" .
		"加群关键词: AUTO_ALLOW_KEY\t\t\t" . $this->getParamValue('AUTO_ALLOW_KEY') . "\n" .
		"服务关闭语: SERVICE_OPEN_MESS\t\t\t" . $this->getParamValue('SERVICE_OPEN_MESS') . "\n" .
		"本群字号体: CLUSTER_FONT_MESS\t\t\t" . $this->getParamValue('CLUSTER_FONT_MESS') . "\n" .
		"发言最多的: REPORT_NUMBER_ONE\t\t\t" . $this->getParamValue('REPORT_NUMBER_ONE') . "\n" .
		"获得成就  : REPORT_NUMBER_ONE_MESS\t\t" . $this->getParamValue('REPORT_NUMBER_ONE_MESS') . "\n" .
		"关键字警告: FILTER_WORD_WARNING_MESS\t\t" . $this->getParamValue('FILTER_WORD_WARNING_MESS') . "\n" .
		"关键字提人: FILTER_WORD_T_MESS\t\t\t" . $this->getParamValue('FILTER_WORD_T_MESS') . "\n" .
		"\n修改参数值的方式：\n" .
		"@param  参数类型  参数值  例如：\n" .
		"@param  RECEIVENEWMEMBERADD_MESS   大家欢迎新成员：{0}[{1}]加入群：{2}[{3}] 群 请遵守群规哦~！ \n" .
		"要恢复默认值请把参数值设为 -1 \n例如：@param  RECEIVENEWMEMBERADD_MESS  -1 \n" .
		" 有问题请 @丫丫 -m  你要说的话";
		return;
	}

	public function getMyParamValue($param_name) {
		$ret = @ DB :: LimitQuery('td_my_param', array (
			'condition' => array (
				'qq' => $_POST[Sender],
				'key' => $param_name
			)
		), true);
		if ($ret) {
			return $ret[0][value];
		} else {
			return false;
		}

	}

	public function saveMyParamValue($param_name, $value) {
		if (!$this->getMyParamValue($param_name)) {
			$arr = array (
				'key' => $param_name,
				'value' => $value,
				'qq' => $_POST[Sender]
			);
			DB :: SaveTableRow('td_my_param', $arr);
			return "添加参个人数成功";
		} else {
			$arr = array (
				'key' => $param_name,
				'value' => $value,
				'qq' => $_POST[Sender]
			);
			$id = array (
				'key' => $param_name,
				'qq' => $_POST[Sender]
			);
			DB :: Update('td_my_param', $id, $arr);
			return "修改参个人数成功";
		}

	}

	private function __get($property_name) {
		return $this-> $property_name;

	}

	private function __set($property_name, $value) {
		$this->$property_name = $value;
	}
}
?>